import 'package:flutter/material.dart';
import 'package:untitled5/bor.dart';

import 'friends_data.dart';

class _FriendCell extends StatelessWidget {
  _FriendCell({
    required this.name,
    required this.imageUrl,
    this.groupTitle = '',
    required this.imageAssets,
    this.xian = 'T',
  });

  final String imageUrl;
  final String name;
  final String groupTitle;
  final String imageAssets;
  final String xian;

  @override
  Widget build(BuildContext context) {
    // final GlobalKey key = GlobalKey();
    // RenderBox renderBox = key.currentContext?.findRenderObject() as RenderBox;
    // double widgetHeight = renderBox.size.height;
    // print(widgetHeight);
    return Column(
      key: key,
      children: [
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 10),
          height: groupTitle != '' ? 30 : 0,
          color: Color.fromRGBO(237, 237, 237, 1.0),
          child: Text(groupTitle != '' ? groupTitle : ''),
        ),
        Container(
          color: Colors.white,
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    margin: EdgeInsets.all(10),
                    width: 32,
                    height: 32,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(6),
                      image: DecorationImage(
                        image: imageUrl != ''
                            ? NetworkImage(imageUrl)
                            : AssetImage(imageAssets) as ImageProvider<Object>,
                      ),
                    ),
                  ),
                  Container(
                    child: Text(name),
                  ),
                ],
              ),
              xian == 'T'
                  ? Row(
                      children: [
                        Container(
                          width: 45,
                          height: .8,
                          color: Colors.white,
                        ),
                        Container(
                          width: MediaQuery.of(context).size.width - 45,
                          height: .8,
                          color: Color.fromRGBO(237, 237, 237, 1.0),
                        )
                      ],
                    )
                  : Container()
            ],
          ),
        ),
      ],
    );
  }
}

class FriendsPage extends StatefulWidget {
  const FriendsPage({super.key});

  @override
  State<FriendsPage> createState() => _FriendsPageState();
}

class _FriendsPageState extends State<FriendsPage> {
  final List _headerData = [
    Friends(
      imageAssets: 'images/newfriends.png',
      name: '新的朋友',
      imageUrl: '',
    ),
    Friends(
      imageAssets: 'images/qunliao.png',
      name: '群聊',
      imageUrl: '',
    ),
    Friends(
      imageAssets: 'images/biaoqian.png',
      name: '标签',
      imageUrl: '',
    ),
    Friends(
      imageAssets: 'images/gongzhonghao.png',
      name: '公众号',
      imageUrl: '',
    ),
  ];

  final Map _map = {words_index[0]: 0.0, words_index[1]: 0.0};
  double cellHeight = 52.8;
  double cellHeart = 30.0;

  late ScrollController _scrollController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _scrollController = ScrollController();

    var zHeight = cellHeight * 4;
    for (var i = 0; i < datas.length; i++) {
      if (i < 1) {
        zHeight += cellHeight + cellHeart;
      } else if (datas[i].indexLetter == datas[i - 1].indexLetter) {
        zHeight += cellHeight;
      } else {
        zHeight += cellHeight + cellHeart;
      }
    }

    var get = cellHeight * _headerData.length;

    for (var i = 0; i < datas.length; i++) {
      if (i < 1) {
        _map.addAll({datas[i].indexLetter: get});
        get += cellHeight + cellHeart;
      } else if (datas[i].indexLetter == datas[i - 1].indexLetter) {
        get += cellHeight;
      } else {
        _map.addAll({datas[i].indexLetter: get});
        get += cellHeight + cellHeart;
      }
    }
  }

  Widget _itemForRow(BuildContext context, int index) {
    if (index < _headerData.length) {
      return _FriendCell(
        name: _headerData[index].name,
        imageAssets: _headerData[index].imageAssets,
        imageUrl: '',
      );
    }
    //不显示
    if (index - 4 > 0 &&
        datas[index - 5].indexLetter == datas[index - 4].indexLetter) {
      return _FriendCell(
        name: datas[index - 4].name,
        imageUrl: datas[index - 4].imageUrl,
        imageAssets: datas[index - 4].imageAssets,
      );
    }
    return _FriendCell(
        name: datas[index - 4].name,
        imageUrl: datas[index - 4].imageUrl,
        groupTitle: datas[index - 4].indexLetter,
        imageAssets: datas[index - 4].imageAssets);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(237, 237, 237, 1.0),
        elevation: 0,
        title: Text('通讯录'),
      ),
      body: Stack(children: [
        Container(
          color: Color.fromRGBO(237, 237, 237, 1.0),
          child: ListView.builder(
            controller: _scrollController,
            itemBuilder: _itemForRow,
            itemCount: datas.length + _headerData.length,
          ),
        ),
        //索引条
        Bar(
          BarBack: (String str) {
            if (_map[str] != null) {
              _scrollController.animateTo(_map[str],
                  duration: Duration(microseconds: 200), curve: Curves.easeIn);
              if (_map[str] > _scrollController.position.maxScrollExtent) {
                _scrollController.animateTo(
                    _scrollController.position.maxScrollExtent,
                    duration: Duration(microseconds: 200),
                    curve: Curves.easeIn);
              }
            }
          },
        ),
      ]),
    );
  }
}

// imageUrl.isNotEmpty
// ? NetworkImage(imageUrl)
// : AssetImage(imageAssets),

const words_index = [
  '↑',
  'A',
  'B',
  'C',
  'D',
  'E',
  'F',
  'G',
  'H',
  'I',
  'J',
  'K',
  'L',
  'M',
  'N',
  'O',
  'P',
  'Q',
  'R',
  'S',
  'T',
  'U',
  'V',
  'W',
  'S',
  'Y',
  'Z',
  '#'
];
